
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 11/21/2010 17:31:16
-- Generated from EDMX file: C:\src\snap2010\SnapModel\SnapModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Snap];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ExchangeCompany]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Companies] DROP CONSTRAINT [FK_ExchangeCompany];
GO
IF OBJECT_ID(N'[dbo].[FK_NewsItemNewsToCompany]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NewsToCompanies] DROP CONSTRAINT [FK_NewsItemNewsToCompany];
GO
IF OBJECT_ID(N'[dbo].[FK_CompanyNewsToCompany]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NewsToCompanies] DROP CONSTRAINT [FK_CompanyNewsToCompany];
GO
IF OBJECT_ID(N'[dbo].[FK_CompanyTicker]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Tickers] DROP CONSTRAINT [FK_CompanyTicker];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Exchanges]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Exchanges];
GO
IF OBJECT_ID(N'[dbo].[Companies]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Companies];
GO
IF OBJECT_ID(N'[dbo].[NewsItems]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NewsItems];
GO
IF OBJECT_ID(N'[dbo].[NewsToCompanies]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NewsToCompanies];
GO
IF OBJECT_ID(N'[dbo].[Tickers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Tickers];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Exchanges'
CREATE TABLE [dbo].[Exchanges] (
    [Name] nvarchar(10)  NOT NULL
);
GO

-- Creating table 'Companies'
CREATE TABLE [dbo].[Companies] (
    [Symbol] nvarchar(10)  NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [LastSale] decimal(18,0)  NULL,
    [MarketCap] decimal(18,0)  NULL,
    [IpoYear] int  NULL,
    [Sector] nvarchar(max)  NULL,
    [Industry] nvarchar(max)  NULL,
    [ExchangeName] nvarchar(10)  NOT NULL,
    [SummaryQuoteUrl] nvarchar(max)  NOT NULL,
    [IsRussell1000] bit  NOT NULL,
    [TickerId] int  NOT NULL
);
GO

-- Creating table 'NewsItems'
CREATE TABLE [dbo].[NewsItems] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Link] nvarchar(max)  NOT NULL,
    [PublicationDate] datetime  NOT NULL,
    [Body] nvarchar(max)  NOT NULL,
    [Source] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'NewsToCompanies'
CREATE TABLE [dbo].[NewsToCompanies] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [NewsItemId] int  NOT NULL,
    [CompanySymbol] nvarchar(10)  NOT NULL
);
GO

-- Creating table 'Tickers'
CREATE TABLE [dbo].[Tickers] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Tick] bigint  NOT NULL,
    [BuyVolume] int  NOT NULL,
    [SellVolume] int  NOT NULL,
    [BuyPriceAvg] decimal(18,5)  NOT NULL,
    [SellPriceAvg] decimal(18,5)  NOT NULL,
    [CompanySymbol] nvarchar(10)  NOT NULL,
    [MarketDate] datetime  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Name] in table 'Exchanges'
ALTER TABLE [dbo].[Exchanges]
ADD CONSTRAINT [PK_Exchanges]
    PRIMARY KEY CLUSTERED ([Name] ASC);
GO

-- Creating primary key on [Symbol] in table 'Companies'
ALTER TABLE [dbo].[Companies]
ADD CONSTRAINT [PK_Companies]
    PRIMARY KEY CLUSTERED ([Symbol] ASC);
GO

-- Creating primary key on [Id] in table 'NewsItems'
ALTER TABLE [dbo].[NewsItems]
ADD CONSTRAINT [PK_NewsItems]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'NewsToCompanies'
ALTER TABLE [dbo].[NewsToCompanies]
ADD CONSTRAINT [PK_NewsToCompanies]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Tickers'
ALTER TABLE [dbo].[Tickers]
ADD CONSTRAINT [PK_Tickers]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ExchangeName] in table 'Companies'
ALTER TABLE [dbo].[Companies]
ADD CONSTRAINT [FK_ExchangeCompany]
    FOREIGN KEY ([ExchangeName])
    REFERENCES [dbo].[Exchanges]
        ([Name])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ExchangeCompany'
CREATE INDEX [IX_FK_ExchangeCompany]
ON [dbo].[Companies]
    ([ExchangeName]);
GO

-- Creating foreign key on [NewsItemId] in table 'NewsToCompanies'
ALTER TABLE [dbo].[NewsToCompanies]
ADD CONSTRAINT [FK_NewsItemNewsToCompany]
    FOREIGN KEY ([NewsItemId])
    REFERENCES [dbo].[NewsItems]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NewsItemNewsToCompany'
CREATE INDEX [IX_FK_NewsItemNewsToCompany]
ON [dbo].[NewsToCompanies]
    ([NewsItemId]);
GO

-- Creating foreign key on [CompanySymbol] in table 'NewsToCompanies'
ALTER TABLE [dbo].[NewsToCompanies]
ADD CONSTRAINT [FK_CompanyNewsToCompany]
    FOREIGN KEY ([CompanySymbol])
    REFERENCES [dbo].[Companies]
        ([Symbol])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompanyNewsToCompany'
CREATE INDEX [IX_FK_CompanyNewsToCompany]
ON [dbo].[NewsToCompanies]
    ([CompanySymbol]);
GO

-- Creating foreign key on [CompanySymbol] in table 'Tickers'
ALTER TABLE [dbo].[Tickers]
ADD CONSTRAINT [FK_CompanyTicker]
    FOREIGN KEY ([CompanySymbol])
    REFERENCES [dbo].[Companies]
        ([Symbol])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CompanyTicker'
CREATE INDEX [IX_FK_CompanyTicker]
ON [dbo].[Tickers]
    ([CompanySymbol]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------